package com.dmz.common.utils.secure;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * @author lee
 */
public class GMUtil {

    public static String eb(String key, String iv, String text) {
        byte[] bytes = null;
        try {
            bytes = SM4CBCUtil.encrypt(SM4CBCUtil.loadKeyBase64(key), SM4CBCUtil.loadIVBase64(iv), text.getBytes(StandardCharsets.UTF_8));
        } catch (Exception e) {
            System.out.println(e.getMessage());
            throw new RuntimeException(e);
        }
        return Base64.getEncoder().encodeToString(bytes);
    }

    public static String db(String key, String iv, String text) {
        byte[] encryptedData = Base64.getDecoder().decode(text);
        byte[] bytes = null;
        try {
            bytes = SM4CBCUtil.decrypt(SM4Util.loadKeyBase64(key), SM4CBCUtil.loadIVBase64(iv), encryptedData);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            throw new RuntimeException(e);
        }
        return new String(bytes, StandardCharsets.UTF_8);
    }

    public static void main(String[] args) {
        String key = "iAWakuRFzbYDdHFTmNHwqw==";
        String iv = "yzSIuFOBZ+rYrmaAHpH2YA==";

        String text = "eT3:13@chi";
        //DK_B104
        //aegPXz///gZeHs5atkzpQQ==

        //rBSdF/Rnynju5oJPXDBujQ==
        System.out.println(eb(key, iv, text));
        String ebt = "Q1CEYeYC+0PE3DtevEQNlQ==";
        String db = db(key, iv, ebt);
        System.out.println(db);

        /*String dfn = "03{date}.DZ".replace("{date}", DateUtil.format(new Date(), DatePattern.PURE_DATE_PATTERN))
                .replace("{yesterday}", DateUtil.format(DateUtil.yesterday(), DatePattern.PURE_DATE_PATTERN));
        System.out.println(dfn);

        String[] fileNames = dfn.split(",");
        for (String fileName : fileNames) {
            String[] se = fileName.split("\\*");
            File[] fs = FileUtil.ls("E:\\\\work\\\\src\\\\test");
            for (File f : fs) {
                if (f.isFile()) {
                    boolean flag = true;

                    for (String s : se) {
                        if (!f.getName().contains(s)) {
                            flag = false;
                            break;
                        }
                    }

                    System.out.println(flag);
                }
            }
        }*/
    }
}
